// pages/myIssue/index.js
const app = getApp();
const db = wx.cloud.database();
const _ = db.command;
import {
	goBack
} from '../../Utils/Utils';

let tempArr = [];				// 记录 HistoryList 索引下标, 方便完结删除操作

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		CustomBar: app.globalData.CustomBar,
		StatusBar: app.globalData.StatusBar, // 状态栏高度
		windowHeight: app.globalData.windowHeight, // 屏幕整体高度
		scrollHeight: 0, // scroll高度
		limit: 5, // 每页多少数据
		type: 0, // 搜索的类型
		HistoryList: [], // 每个对象就是一个帖子
		pictures: [], // 这个是全屏展示图片, 不能用本地图片
		current: 0, // 画廊下标
		PicturesShow: false, // 是否显示图片展示
		BombBoxShow: false, // 加载状态是否显示
		BombBoxText: "努力加载中", // 加载状态文字
		BombBox2Show: false, // 提示状态是否显示
		BombBox3text: "已经完结", // 
		BombBox3Show: false, //
		slideButtons: [{ // 闲置出让,失物招领 选择按钮
			text: '完结',
			extClass: 'slideviewBtn'
		}, {
			text: '删除',
			type: 'warn',
			extClass: 'slideviewBtn'
		}],
		slideButtons_express: [ // 我要表白 选择按钮
			{
				text: '删除',
				type: 'warn',
				extClass: 'slideviewBtn'
			}
		],
		showIndex: -1, // 显示哪一个slide组件
		isOver: false, // 是否完结
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		let that = this
		let CustomBar = that.data.CustomBar
		let windowHeight = that.data.windowHeight
		let temp = windowHeight - CustomBar
		that.setData({
			scrollHeight: temp
		})
		// 加载我的文章列表
		that.loadArticles()
	},

	optionBombBox2Show: function(param) {
		let that = this
		let temp = that.data.BombBox2Show
		let temp2 = that.data.BombBoxShow
		if (temp || temp2) {
			return
		}
		that.setData({
			BombBox2Show: true,
			BombBox2text: param
		})
		setTimeout(() => {
			that.setData({
				BombBox2Show: false,
			})
		}, 1500)
	},

	optionBombBox3Show: function(param) {
		let that = this
		let temp1 = that.data.BombBoxShow
		let temp2 = that.data.BombBox2Show
		let temp3 = that.data.BombBox3Show
		if (temp1 || temp2 || temp3) {
			return
		}
		that.setData({
			BombBox3Show: true,
			BombBox3text: param
		})
		setTimeout(() => {
			that.setData({
				BombBox3Show: false,
			})
		}, 1500)
	},


	openBombBox: function(param) {
		let that = this
		that.setData({
			BombBoxShow: true,
			BombBoxText: param
		})
	},

	closeBombBox: function() {
		let that = this
		that.setData({
			BombBoxShow: false,
		})
	},

	/**
	 * 加载数据
	 */
	loadArticles() {
		let that = this;
		that.openBombBox("加载中...")
		let skip = that.data.HistoryList.length;
		let limit = that.data.limit
		if (skip % limit == 0) {
			// 当skip不为 limit 整除 就说明已经是最后一页了
			//可以尝试获取下一页数据
			wx.cloud.callFunction({
				name: "operation",
				data: {
					function: "getActicleList",
					skip: skip,
					limit: limit,
					type: this.data.type
				}
			}).then(res => {
				let acticlelists = res.result.data;
				// 本地data文章列表
				let datalist = []
				for (let al of acticlelists) {
					datalist.push({
						flagId: al._id,
						type: al.type,
						isOver: al.isOver
					})
				}
				
				that.setData({
					HistoryList: that.data.HistoryList.concat(datalist)
				})
				
				that.closeBombBox()
				that.setTempArr()
			}).catch(err => {
				console.log(err)
				that.closeBombBox()
			})
		} else {
			that.closeBombBox()
			that.optionBombBox2Show("没有喽")
		}
	},

	goBack,

	setTempArr: function(){
		let temp = this.data.HistoryList
		let j=0
		let i=temp.length
		for(; j<i;j++ ){
				if(temp[j].isOver==1){
					tempArr[j]=1
				}
				if(temp[j].isOver==0){
					tempArr[j]=0
				}
				
				if(temp[j].isOver == undefined) {
					tempArr[j]=1
				}
		}
		
	},

	changePic(e) { // 显示隐藏预览图片组件
		let {
			pictures,
			index
		} = e.detail
		this.setData({
			current: index,
			pictures: pictures,
			PicturesShow: true
		})
	},

	hide: function() { // 关闭画廊触发
		this.setData({
			pictures: []
		})
	},

	/**
	 * 闲置出让，失物招领的完结，删除事件
	 * @param {*} e 
	 */
	slideButtonTap(e) {
		let that = this
		let index = e.currentTarget.dataset.index
		let tempObj = that.data.HistoryList[index]
		let HistoryList = that.data.HistoryList
		that.setData({
			showIndex: -1
		})
		let acticleid = e.currentTarget.dataset.acticleid;
		
		if(tempObj.isOver==1){
			that.optionBombBox3Show('已完结')
			return
		}
		
		
		let count = 0
		for(let i=0;i<index;i++){
			if(tempArr[i]==0){
				count++
			}
		}
		
		if (e.detail.index == 0) {
			try {
				db.collection("publishInfo").doc(acticleid)
					.update({
						data: {
							isOver: _.set(1)
						}
					}).then(res => {
						let child = that.selectAllComponents('.bbb ');
						
						
						child[count].setData({
							isOver:1
						})
						that.optionBombBox3Show('已完结')
					})
			} catch (e) {
				console.log(e)
			}
		} else {
			that.deleteActicleById(acticleid, index)
		}
	},

	/**
	 * 我要表白的删除按钮事件
	 * @param {}} e 
	 */
	slideButtonTapExpress(e) {
		let index = e.currentTarget.dataset.index
		this.setData({
			showIndex: -1
		})
		let acticleid = e.currentTarget.dataset.acticleid;
		this.deleteActicleById(acticleid, index)
	},

	/**
	 * 根据文章id删除文章
	 * @param {文章id} acticleId
	 */
	deleteActicleById(acticleId, index) {
		let that = this
		let obj = 'HistoryList[' + index + '].type'

		try {
			// 删除文章
			db.collection("publishInfo").doc(acticleId)
				.remove().then(res => {
					// 删除文章后的操作
					that.setData({
						[obj]: 4,
					})
					that.optionBombBox3Show('已删除')
					that.setTempArr()
				}).catch(err => {
					console.log(err)
				})

			// 删除评论
			wx.cloud.callFunction({
				name: "operation",
				data: {
					function: "deleteAllCommentByActicleId",
					_id: acticleId
				}
			}).then(res => {}).catch(err => {})

			// 删除点赞
			wx.cloud.callFunction({
				name: "operation",
				data: {
					function: "deleteAllPraiseByActicleId",
					_id: acticleId
				}
			}).then(res => {}).catch(err => {})
		} catch (e) {}
	},


	slideviewShow: function(e) { //	slideview显示时触发	
		let {
			index
		} = e.currentTarget.dataset
		this.setData({
			showIndex: index
		})
	},

	// scroll触底事件
	bindscrolltolower() {
		this.loadArticles()
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
